<?php
// 公共函数

// Db对象 库名 索引
function Db($name = null,$index = 0)
{
    if(func_num_args() < 2 && is_int($name)){
        $index = $name;
        $name = null;
    }
    return \yphp\Db::instance($index)->dbn($name);
}

// table方法
function table($table,$dth = null,$name = null,$index = 0)
{
    return Db($name,$index)->table($table,$dth);
}

// 生成随机码
function getNoncestr($num=8)
{
    $arr = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','1','2','3','4','5','6','7','8','9','0'];
    $tmpstr = '';
    $max = count($arr) - 1;
    for($i = 1;$i <= $num;$i++){
        $key = rand(1,$max);
        $tmpstr .= $arr[$key];
    }

    return $tmpstr;
}

// dump
function dump()
{
    foreach (func_get_args() as $arg) {
        echo '<pre style="padding:10px;margin:15px 10px;font-size:14px;line-height:1.5;color:#333;background:#ccc;border:1px solid #ccc;border-radius:4px;overflow-x:auto">';
        var_dump($arg);
        echo '</pre>';
    }
}

// show
function show()
{
    foreach (func_get_args() as $arg) {
        echo '<pre style="padding:10px;margin:15px 10px;font-size:14px;line-height:1.5;color:#333;background:#ccc;border:1px solid #ccc;border-radius:4px;overflow-x:auto">';
        print_r($arg);
        echo '</pre>';
    }
}

/**
 * 验证码生成
 * 可在执行的时候传入配置参数
 * 例子： $configArr = ['num' => 2,'width' => 150,'height' => 40,'size' => 12];
 *       $obj = Captcha::instance($configArr)->imgCode();
 *       Session::set('code', $obj->str);
 *       header('content-type:text/png;');
 *       echo $obj->img;
 */
function captcha_code($code = null)
{
    return is_null($code)?'/captcha.html':"/captcha/$code.html";
}